//
//  SettingVC.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/24.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "SettingVC.h"
#import "BottomTopLineTableViewCell.h"
#import "QuitLoginCell.h"
#import "Macro.h"

#import "NotificationHelp.h"
#import "AccountSysVM.h"

#import "UserSettings.h"
#import "AccountSaveVC.h"

#define SettingVCAccountAndSave @"账号和安全"
#define SettingVCNewNote        @"新消息通知"
#define SettingVCPrivacy        @"隐私"
#define SettingVCHelp           @"帮助中心"
#define SettingVCAbout          @"关于我们"
#define SettingVCQuit           @"退出登录"

@interface SettingVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *settingList;
@property (nonatomic, strong) AccountSysVM *accountSysVM;
@end

@implementation SettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"设置";
    
    self.view.backgroundColor = [EPColor epColor_Bg];
    [self.view addSubview:self.tableView];
    
    [self createViewModel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createViewModel
{
    self.accountSysVM = [[AccountSysVM alloc] init];
    WEAKSELF
    [self.accountSysVM setBlockWithReturnBlock:^(BaseJSONEntity *returnValue) {
        // 统一数据处理
        [weakSelf handelRequestData:returnValue];
    } WithErrorBlock:^(BaseJSONEntity *errorValue) {
        [NotificationHelp postNotification:NotificationGo2LoginVC object:nil];
    } WithFailureBlock:^(BaseJSONEntity *failureValue) {
        [NotificationHelp postNotification:NotificationGo2LoginVC object:nil];
    }];
}

#pragma mark - 登录成功处理
-(void)handelRequestData:(BaseJSONEntity *)returnValue{
    
    [self hideProcessingHud];
    
    // 登出时，不需要处理返回结果
    if (![returnValue isKindOfClass:[NSDictionary class]]) {
        NSLog(@"返回数据异常需要处理");
    }
    [NotificationHelp postNotification:NotificationGo2LoginVC object:nil];
}


- (NSArray *)settingList
{
    if (!_settingList) {
        _settingList = @[@[SettingVCAccountAndSave], @[SettingVCNewNote], @[SettingVCHelp, SettingVCAbout], @[SettingVCQuit]];
    }
    return _settingList;
}

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *subList = [self.settingList objectAtIndex:section];
    return subList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section+1 == self.settingList.count) {
        QuitLoginCell *quitCell = [[QuitLoginCell alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, Cell_H)];
        return quitCell;
    }
    
    static NSString *identifier = @"MyResnVC";
    BottomTopLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[BottomTopLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSArray *setSubList = [self.settingList objectAtIndex:indexPath.section];
    cell.textLabel.text = [setSubList objectAtIndex:indexPath.row];
    cell.textLabel.font = SystemFont(16.f);
    if (indexPath.row + 1 != setSubList.count) {
        cell.bottomLineLeftEdge = Cell_X;
    }
    else
    {
        cell.bottomLineHidden = YES;
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.settingList.count;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return Cell_Spacing_H;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return Cell_H;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *subList = [self.settingList objectAtIndex:indexPath.section];
    NSString *strSelected = [subList objectAtIndex:indexPath.row];
    if ([strSelected isEqualToString:SettingVCAccountAndSave])
    {
        return [self accountAndSave];
    }
    if ([strSelected isEqualToString:SettingVCNewNote])
    {
        return [self newNote];
    }
    if ([strSelected isEqualToString:SettingVCPrivacy])
    {
        return [self privacy];
    }
    if ([strSelected isEqualToString:SettingVCHelp])
    {
        return [self help];
    }
    if ([strSelected isEqualToString:SettingVCAbout])
    {
        return [self about];
    }
    if ([strSelected isEqualToString:SettingVCQuit])
    {
        [MobClick event:@"click_ios_PersonalCenterVC_LogOff"];
        return [self quit];
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, Cell_Spacing_H)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
                         
#pragma mark - 子菜单入口函数
// 账号和安全
- (void)accountAndSave
{
    AccountSaveVC *accSaveVC = [[AccountSaveVC alloc] init];
    accSaveVC.title = SettingVCAccountAndSave;
    [self.navigationController pushViewController:accSaveVC animated:YES];
}
// 新消息通知
- (void)newNote
{
}
// 隐私
- (void)privacy
{
}
// 帮助中心
- (void)help
{
}
// 关于我们
- (void)about
{
}
// 退出登录
- (void)quit
{
    if (nil == UserSaveInfo.token || 0 == UserSaveInfo.token.length) {
        [NotificationHelp postNotification:NotificationGo2LoginVC object:nil];
        return;
    }
    
    [self showProcessingHud:@"..."];
    [self.accountSysVM logoutRequestWithToken:UserSaveInfo.token];
}

@end
